//
//  BDAddFriendViewController.m
//  BadBoy
//
//  Created by wen on 2017/4/13.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDAddFriendViewController.h"
#import "UIViewController+SearchController.h"
#import "BDUserModel.h"
#import "BDUserInfoViewController.h"
#import "BDNewFriendTableViewCell.h"
@interface BDAddFriendViewController ()<EMSearchControllerDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *searchResultDatasource;

@end

@implementation BDAddFriendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = ASLocalizedString(@"添加好友");
    
    self.hidesNavigationBarDuringPresentation = YES;
    [self enableSearchController];
    self.resultController.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableHeaderView = self.searchController.searchBar;
    self.searchController.searchBar.placeholder = ASLocalizedString(@"搜索联系人");
    
    __block BDAddFriendViewController *weafSelf = self;
    self.resultController.numberOfRowsInSectionCompletion = ^NSInteger(UITableView *tableView, NSInteger section) {
        return weafSelf.searchResultDatasource.count;
    };
    
    self.resultController.cellForRowAtIndexPathCompletion = ^(UITableView *tableView, NSIndexPath *indexPath) {
        BDNewFriendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"aaa"];
        if (cell == nil) {
            cell = [[BDNewFriendTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"aaa"];
        }
        BDUserModel *user = weafSelf.searchResultDatasource[indexPath.row];
        cell.textLabel.text = user.nickname;
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:user.cover] placeholderImage:[UIImage imageNamed:@"avatar"]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row == 0) {
            cell.isFirst = YES;
        }else{
            cell.isFirst = NO;
        }
        return cell;
    };
    
    self.resultController.heightForRowAtIndexPathCompletion = ^CGFloat(UITableView *tableView, NSIndexPath *indexPath) {
        return 60.0;
    };
    
    self.resultController.didSelectRowAtIndexPathCompletion = ^(UITableView *tableView, NSIndexPath *indexPath) {
        BDUserModel *current_user = weafSelf.searchResultDatasource[indexPath.row];
        BDUserInfoViewController *userVC = [[BDUserInfoViewController alloc] initWithUserID:current_user.user_id isFollow:NO guanzhuBlock:nil];
        [weafSelf.navigationController pushViewController:userVC animated:YES];
    };
}

- (void)searchTextChangeWithString:(NSString *)aString{
    
    if ([aString isNotEmpty]) {
        self.startPage = @"1";
        NSString *url = [NSString stringWithFormat:@"%@users?page=%@&size=%@&type=1&keyword=%@&no_need_friends=1", KBDBaseURL, self.startPage, self.pageSize, aString];
        url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSString *headValid = [BDUserModel getHeaderValidation];
        RACSignal *signal = [[BDNetworkingServer yy_GET:url parameters:nil validation:headValid automaticProcessingError:YES] execute:nil];
        [signal subscribeNext:^(id  _Nullable x) {
            self.isEnd = [x[@"is_end"] boolValue];
            self.startPage = x[@"next_page"];
            self.searchResultDatasource = [BDUserModel mj_objectArrayWithKeyValuesArray:x[@"list"]];
            [self.resultController.tableView reloadData];
            [self setTableiewFooterViewStateFor:self.resultController.tableView datasourceCount:self.searchResultDatasource.count];
        }];
        
        [signal subscribeError:^(NSError * _Nullable error) {
            // BDLog(ASLocalizedString(@"搜索失败，错误是：%@"), error);
        }];
    }
}

- (UITableView *)tableView{
    if (!_tableView) {
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kBDTopHeight, kBDScreenWidth, kBDScreenHeight - kBDTopHeight) style:UITableViewStylePlain];
        _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        _tableView.backgroundColor = [UIColor clearColor];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
